package com.schoolhelper.comment.mapper;

import com.schoolhelper.comment.domain.Comment;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface CommentMapper {
    
    // 根据帖子ID获取评论列表（包含用户信息）
    List<Comment> getCommentsByPostId(@Param("postId") Long postId);
    
    // 根据帖子ID获取评论列表（分页）
    List<Comment> getCommentsByPostIdWithPagination(@Param("postId") Long postId, @Param("offset") int offset, @Param("size") int size);
    
    // 插入评论
    @Insert("INSERT INTO comment (post_id, user_id, content, parent_id, is_deleted, create_time, update_time) " +
            "VALUES (#{postId}, #{userId}, #{content}, #{parentId}, 0, NOW(), NOW())")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertComment(Comment comment);
    
    // 软删除评论
    @Update("UPDATE comment SET is_deleted = 1, update_time = NOW() WHERE id = #{commentId}")
    int deleteComment(@Param("commentId") Long commentId);
    
    // 根据评论ID获取评论信息
    Comment getCommentById(@Param("commentId") Long commentId);
    
    // 更新帖子评论数量
    @Update("UPDATE post SET comment_count = comment_count + 1 WHERE id = #{postId}")
    int incrementCommentCount(@Param("postId") Long postId);
    
    @Update("UPDATE post SET comment_count = comment_count - 1 WHERE id = #{postId} AND comment_count > 0")
    int decrementCommentCount(@Param("postId") Long postId);
}